<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of PagerFrontEnd
 *
 * @author Administrator
 */
class PagerFrontEnd {

    public static function getPagerData($numHits, $limit, $page) {
        //kiem tra du lieu nhap
        $numHits = (int) $numHits; //tong so mau tin
        $limit = max((int) $limit, 1); //so mau tin moi trang
        $page = (int) $page; //so trang dang duyet
        $numPages = ceil($numHits / $limit); // tinh tong so trang
        $page = max($page, 1); //neu page < 1 thì page = 1
        $page = min($page, $numPages); //neu page>$numPages thì page=$numPages
        $offset = ($page - 1) * $limit;

        $view = '';
        if (isset($_GET["view"])) {
            $view = $_GET["view"];
        }

        if ($view == "category") {
            if ($_GET["com"] == "com_content") {
                $selfUrl = "./com_content/category/" . $_GET["alias"] . "/" . $_GET["item"];
            }
            else if ($_GET["com"] == "com_item") {
                $selfUrl = "./com_item/category/" . $_GET["alias"] . "/" . $_GET["item"];
            }
        }

        if ($view == "categories") {
            if ($_GET["com"] == "com_content") {
                $selfUrl = "./com_content/categories/" . $_GET["alias"] . "/" . $_GET["item"];
            }
            else if ($_GET["com"] == "com_item") {
                $selfUrl = "./com_item/categories/" . $_GET["alias"] . "/" . $_GET["item"];
            }
        }
        else if ($view == "article") {
            $selfUrl = "./com_content/article/" . $_GET["alias"] . "/" . $_GET["item"];
        }
        if ($view == "") {
            $selfUrl = "./com_new_product/new_product_list/san-pham-moi/san-pham-moi";
        }
        if ($view == "new_product_list") {
            $selfUrl = "./com_new_product/new_product_list/san-pham-moi/san-pham-moi";
        }
        if ($view == "session") {
            $selfUrl = "./com_content/session/" . $_GET["alias"] . "/" . $_GET["item"];
        }
        else if ($view == "article") {
            $selfUrl = "./com_content/article/" . $_GET["alias"] . "/" . $_GET["item"];
        }

        $maxP = 2;
        $revpage = $page - 1;
        $nexpage = $page + 1;
        $paging = '';
        if ($page == 1 || $page == 0) {
            $paging .= "<li class=\"page_text\"><a href=\"javascript:void(0);\"> <img src=\"templates/default/images/FirstDisabled.jpg\"> </a></li>";
            $paging .= "<li class=\"page_text\"><a href=\"javascript:void(0);\"> <img src=\"templates/default/images/PreviousDisabled.jpg\"> </a></li>";
        }
        else {
            $paging = $paging . "<li class=\"page_text\"><a href=\"$selfUrl/1.htm\" title=\"Trang đầu\"><img src=\"templates/default/images/First.jpg\"></a></li>";
            $paging = $paging . "<li class=\"page_text\"><a href=\"$selfUrl/$revpage.htm\" title=\"Trang kế\"><img src=\"templates/default/images/Previous.jpg\"></a></li>";
        }

        $page = $page;
        $pages = $numPages;
        $hien_tai = $page;
        $bien_trai = $hien_tai - $maxP;
        $bien_phai = $hien_tai + $maxP;

        if ($bien_trai < 1)
            $bien_trai = 1;

        if ($bien_phai > $pages)
            $bien_phai = $pages;

        if ($bien_trai > 1)
            $paging = $paging . "<li class=\"page_img\"><a href=\"$selfUrl/1.htm\">1 </a></li>";

        if ($bien_trai > 2)
            $paging = $paging . " <li class=\"page_text\"> ... </li> ";

        for ($i = $bien_trai; $i <= $bien_phai; $i++) {
            if ($i == $hien_tai)
                $paging = $paging . "<li class=\"page_active\"><a href=\"javascript:void(0)\">$i </a></li>";
            else
                $paging = $paging . "<li class=\"page_img\"><a href=\"$selfUrl/$i.htm\">$i </a></li>";
        }

        if ($bien_phai < $pages)
            $paging = $paging . " <li class=\"page_text\"> ... </li> ";

        if ($bien_phai < $pages - 1)
            $paging = $paging . "<li class=\"page_img\"><a href=\"$selfUrl/$pages.htm\">$pages </a></li>";

        if ($page == $pages) {
            $paging = $paging . "<li class=\"page_text\"><a href=\"javascript:void(0);\"><img src=\"templates/default/images/NextDisabled.jpg\"></a></li>";
            $paging = $paging . "<li class=\"page_text\"><a href=\"javascript:void(0);\"><img src=\"templates/default/images/LastDisabled.jpg\"></a></li>";
        }
        else {
            $paging = $paging . "<li class=\"page_text\"><a href=\"$selfUrl/$nexpage.htm\" title=\"Trang sau\"><img src=\"templates/default/images/Next.jpg\"></a></li>";
            $paging = $paging . "<li class=\"page_text\"><a href=\"$selfUrl/$pages.htm\" title=\"Trang cuối\"><img src=\"templates/default/images/Last.jpg\"></a></li>";
        }

        $ret = new stdClass;
        $ret->offset = $offset;
        $ret->limit = $limit + $offset;
        $ret->numPages = $numPages;
        $ret->page = $page;
        $ret->paging = $paging;
        return $ret;
    }

}

?>
